from django.shortcuts import render

# Create your views here.

from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from django.conf import settings

from users.models import CommunicationModel,OrderModel,MsgModel
from .serializer import CommunicationSer



# 沟通表
class Communication_View(ViewSet):

    # 获取聊天列表
    def list(self, request):
        try:
            user_id = request.query_params.get("user_id")
            user_type = request.query_params.get("user_type")
            # user_id = request.data.get('id')
            # user_type = request.data.get('user_type')
            print(user_id,user_type)
            if user_type == 2:
                comm_list = CommunicationModel.objects.filter(shops_id=user_id, is_delete=False).all()
            else:
                comm_list = CommunicationModel.objects.filter(user_id=user_id, is_delete=False).all()
            comm_list = CommunicationSer(comm_list, many=True)
            return Response({"errcode": 0, "msg": "ok！", "data": comm_list.data})
        except Exception as e:
            print("错误", e)
            return Response({"errcode": 1, "msg": "服务器错误！"})

    # 用户商家，建立沟通
    def add(self, request, pk):
        try:
            user_id = request.data.get("user_id")
            comm = CommunicationModel.objects.filter(user_id=user_id, shops_id=pk, is_delete=False).first()
            if not comm:
                comm = CommunicationModel.objects.create(user_id=user_id, shops_id=pk)
            comm = CommunicationSer(comm)
            return Response({"errcode": 0, "msg": "ok！", "data": comm.data})
        except Exception as e:
            print("错误", e)
            return Response({"errcode": 1, "msg": "服务器错误！"})

# 消息表
# class Msg_View(ViewSet):
#     # 获取聊天记录
#     def list(self, request, pk):
#         try:
#             user_id = request.query_params.get("user_id")
#             user_type = request.query_params.get("user_type")
#             print(user_id,user_type)
#             if user_type == 2:
#                 comm_list = MsgModel.objects.filter(shops_id=user_id, is_delete=False).all()
#             else:
#                 comm_list = CommunicationModel.objects.filter(user_id=user_id, is_delete=False).all()
#             comm_list = CommunicationSer(comm_list, many=True)
#             return Response({"errcode": 0, "msg": "ok！", "data": comm_list.data})
#         except Exception as e:
#             print("错误", e)
#             return Response({"errcode": 1, "msg": "服务器错误！"})

